﻿// -----------------------------------------------------------------------
// <copyright file="_CreateView.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ConsoleApplication1.YIBIYIMVC
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DotNet.Schema;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public partial class Service
    {
        public string NameSpacePR;
        public TableInfo DBTable;

        public Service(TableInfo t, string ns)
        {
            if (t == null)
                throw new Exception("参数t，不能为null。");

            this.NameSpacePR = ns;
            this.DBTable = t;
        }

        public string GetKeyParamterString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < this.DBTable.Keys.Count; i++)
            {
                if (i > 0)
                {
                    sb.Append(", ");
                }
                var item = this.DBTable.Keys[i];
                sb.AppendFormat("{0} {1}", item.CSharpType, item.PascalName);
            }
            return sb.ToString();
        }

        public string GetUpdateFilterString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < this.DBTable.Keys.Count; i++)
            {
                if (i > 0)
                {
                    sb.Append(" && ");
                }
                var item = this.DBTable.Keys[i];
                sb.AppendFormat("p.{0} == dto.{0}", item.PascalName);
            }
            return sb.ToString();
        }

        public string GetDeleteFilterString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < this.DBTable.Keys.Count; i++)
            {
                if (i > 0)
                {
                    sb.Append(" && ");
                }
                var item = this.DBTable.Keys[i];
                sb.AppendFormat("p.{0} == {0}", item.PascalName);
            }
            return sb.ToString();
        }

        public string GetCanUpdateFilterString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < this.DBTable.Keys.Count; i++)
            {
                if (i > 0)
                {
                    sb.Append(" && ");
                }
                var item = this.DBTable.Keys[i];
                sb.AppendFormat("p.{0} != dto.{0}", item.PascalName);
            }
            return sb.ToString();
        }

        public string GetSearchTextFilterString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < this.DBTable.Columns.Count; i++)
            {
                var item = this.DBTable.Columns[i];
                if (item.CSharpType == "string")
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(" || ");
                    }

                    sb.AppendFormat("p.{0}.Contains(text)", item.PascalName);
                }
            }
            return sb.ToString();
        }

        public string GetOrderByString()
        {   
            if (this.DBTable.Keys != null && this.DBTable.Keys.Count == 1)
            {
                var item = this.DBTable.Keys[0];
                if (item.Identity)
                {
                    return string.Format("p.{0}", item.PascalName);
                }
            }

            List<ColumnInfo> list = new List<ColumnInfo>();
            ColumnInfo createTimeCI = null;
            ColumnInfo updateTimeCI = null;
            for (int i = 0; i < this.DBTable.Columns.Count; i++)
            {
                var item = this.DBTable.Columns[i];
                if (item.CSharpType == "DateTime")
                {
                    if (item.LowerName.Contains("create") || item.LowerName.Contains("add"))
                    {
                        createTimeCI = item;
                    }
                    if (item.LowerName.Contains("update") || item.LowerName.Contains("modified")
                        || item.LowerName.Contains("modify"))
                    {
                        updateTimeCI = item;
                    }
                    list.Add(item);
                }
            }
            
            if(updateTimeCI != null)
            {
                return string.Format("p.{0}", updateTimeCI.PascalName);
            }
            if (createTimeCI != null)
            {
                return string.Format("p.{0}", createTimeCI.PascalName);
            }

            if (list.Count > 0)
            {
                return string.Format("p.{0}", list[0].PascalName);
            }
            return "";
        }
    }
}
